import http from "../../common/js/http.js"
// 上次启动时的用户信息
let userInfoHistory = uni.getStorageSync('userInfo') || {};
let cur_store = uni.getStorageSync('cur_store') || {};
let infoAuth = uni.getStorageSync('infoAuth') || false;
let mobileAuth = uni.getStorageSync('mobileAuth') || false;
let waitTaskList = []; //等待运行的任务列表
let historySearch = uni.getStorageSync('historySearch') || [];
// let location = uni.getStorageSync('location') || {}
let location = {};

let state = {
		//是否已经登录
		hasLogin: false,
		//用户信息
		info: userInfoHistory,
		infoAuth: infoAuth,
		mobileAuth: mobileAuth,
		location: location,
		meetConfig: {},
		historySearch: historySearch,
		cur_store:{}
	},
	getters = {
		info(state) {
			return state.info;
		},
		cur_store(state) {
			return state.cur_store;
		},
		hasLogin(state) {
			return state.hasLogin;
		},
		infoAuth(state) {
			return state.infoAuth;
		},
		mobileAuth(state) {
			return state.mobileAuth
		},
		location(state) {
			return state.location;
		},
		meetConfig(state) {
			return state.meetConfig;
		},
		historySearch(state) {
			return state.historySearch;
		}
	},
	mutations = {
		login(state, info) { //登录成功后的操作
			//原有的结合传来的参数
			let _info = state.info;
			state.info = Object.assign({}, _info, info);
			//设置为已经登录
			state.hasLogin = !state.info.nickname ? false : true;

			//console.log('state.info', state.info);
			//存储最新的用户数据到本地持久化存储
			uni.setStorageSync('userInfo', state.info);
			if (info.token) {
				uni.setStorageSync('app_token', state.info.token)
				uni.setStorageSync('app_token_expired', state.info.expiretime)
			}

			if (!state.info.nickname) {
				state.infoAuth = false;
			} else {
				state.infoAuth = true;
			}

			if (!state.info.mobile) {
				state.mobileAuth = false;
			} else {
				state.mobileAuth = true;
			}

			uni.setStorageSync('infoAuth', state.infoAuth)
			uni.setStorageSync('mobileAuth', state.mobileAuth)
		},
		logout(state) {
			state.info = {};
			state.hasLogin = false;
			uni.setStorageSync('userInfo', {});
			uni.removeStorageSync('app_token');
			uni.setStorageSync('app_token_expired', 0);
		},
		location(state, data) {
			let location = state.location
			state.location = Object.assign({}, location, data);
			uni.setStorageSync('location', state.location)
		},
		cur_store(state, data){
			let cur_store = state.cur_store
			state.cur_store = Object.assign({}, cur_store, data);
			uni.setStorageSync('cur_store', state.cur_store)
		},
		config(state, data) {
			state.meetConfig = Object.assign({}, state.meetConfig, data)
		},
		historySearch(state, data) {
			if (data) {
				let historySearch = state.historySearch || []

				let index = historySearch.indexOf(data)

				if (index === -1) {
					historySearch.push(data)

					state.historySearch = historySearch

					uni.setStorageSync('historySearch', historySearch)
				}
			}
		},
		clearHistorySearch(state, data) {
			let historySearch = state.historySearch || []
			if (data.type == 1) {
				state.historySearch = []
				uni.setStorageSync('historySearch', [])
			} else {
				historySearch.splice(data.index, 1)

				state.historySearch = historySearch

				uni.setStorageSync('historySearch', historySearch)
			}
		}
	},
	actions = {
		login(context, params) {
			uni.showLoading({
				title: '加载中...',
				mask: true
			})

			//微信登录
			let token = uni.getStorageSync('app_token') || ''
			if (token) {
				http.get('token/check', {}).then(res => {
					uni.hideLoading()
					if (res.code === 1) {
						context.commit('login', res.data.userInfo)
						runTask()
					}
				}, fail => {
					if (fail.statusCode === 401) {
						uniLogin(function(data) {
							context.commit('login', data.userInfo)
							runTask()
						})
					} else {
						uni.hideLoading()
					}
				})
			} else {
				uniLogin(function(data) {
					context.commit('login', data.userInfo)
					runTask()
				})
			}
		},
		config(context, params) {
			http.get('meet.index/getConfig', {}).then(res => {
				if (res.code === 1) {
					context.commit('config', res.data)
				}
			}, fail => {})
		},
		checkLogin(context, params) {
			let token = uni.getStorageSync('app_token') || ''

			if (token) {
				params.callback && params.callback()
			} else {
				params.callback && waitTaskList.push(params.callback)
			}
		},
		logout(context) {

		}
	}

/**
 * 登录
 * @param {Object} callback
 */
function uniLogin(callback) {
	uni.login({
		success: ret => {
			http.get('meet.user/login', {
				code: ret.code,
			}).then(res => {
				uni.hideLoading()
				if (res.code === 1) {
					callback && callback(res.data)
				}

			}, fail => {
				uni.hideLoading()
			})
		},
		fail: () => {
			uni.hideLoading()
		},
		complete: () => {}
	});
}

/**
 * 运行等待的任务
 */
function runTask() {
	if (waitTaskList.length > 0) {
		waitTaskList.forEach(item => {
			item && item()

		})

		waitTaskList = []
	}
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}